from cmdb.settings import *
from utils import  *
from models import Server

# 定时任务，定时扫描
def scan_hosts():
    # 访问所有要扫描的网段/IP
    for host in scanhosts:
        print("正在扫描%s......" %(host))
        # 获取所有可以ping通的主机IP
        active_hosts = scan_active_hosts(host)
        # 一次遍历判断ssh服务是否开启
        for active_host in active_hosts:
            if is_ssh_open(active_host):
                server = Server()
                # 设置IP地址
                server.IP = active_host
                # 执行指令
                for attr, command in commands.items():
                    # attr ='hostname' , command = 'hostname'
                    # 存储主机名、操作系统.....指令执行的结果
                    result = login_ssh(active_host, 'root', 'westos', cmd =command)
                    setattr(server, attr, result)
                server.save()